package com.own.component.spring.doc.config;

import com.own.component.autoconfig.AutoConfigInstance;
import com.own.component.autoconfig.property.swagger.SwaggerProperties;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * SpringDocConfig
 *
 * @author chenxueli
 * @date 2022-10-04 14:32:00
 */
@SpringBootConfiguration
public class SpringDocConfig {

    @Resource
    private SwaggerProperties swaggerProperties;

    @Bean
    public OpenAPI openApiInfo() {
        var tokenRequestField = AutoConfigInstance.INSTANCE.tokenProperties.getTokenRequestField();
        return new OpenAPI()
                .info(buildInfo())
                .externalDocs(
                        new ExternalDocumentation()
                                .description("SpringBoot实战电商项目mall（50K+Star）全套文档")
                                .url("http://www.macrozheng.com")
                )
                .components(
                        new Components()
                                .addSecuritySchemes(tokenRequestField,
                                        new SecurityScheme()
                                                .type(SecurityScheme.Type.HTTP)
                                                .scheme("bearer")
                                                .in(SecurityScheme.In.HEADER)
                                                .bearerFormat("JWT")
                                                .name("test")
                                )
                )
                .addSecurityItem(
                        new SecurityRequirement().addList(tokenRequestField)
                );
    }

    /**
     * 创建联系人信息
     *
     * @return 联系人信息
     */
    private Contact buildContact() {
        return new Contact()
                .name(swaggerProperties.getAuthor())
                .url(swaggerProperties.getUrl())
                .email(swaggerProperties.getEmail());
    }

    /**
     * 创建许可证信息
     *
     * @return 许可证信息
     */
    private License buildLicense() {
        return new License()
                .name(swaggerProperties.getLicense())
                .url(swaggerProperties.getLicenseUrl());
    }

    /**
     * 创建基本信息
     *
     * @return 基本信息
     */
    private Info buildInfo() {
        return new Info()
                .contact(buildContact())
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .version(swaggerProperties.getVersion())
                .license(buildLicense());
    }

}
